﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.Model;
using YouHP.BLL;

namespace YouHP.Pages.User
{
   public class SetPassword:BasePage
    {
       protected UserInfo _User=new UserInfo();
       bool succ=false;
       protected override void ShowPage()
       {
           base.ShowPage();
           if (ErrorCount != 0) return;
           HandleCommon();
           if (IsPost) HandlePost();
           else HandleGet();
       }

       private void HandleGet()
       {
           
       }

       private void HandlePost()
       {
           string password = RequestTools.GetFormString("newpassword");
           string password1 = RequestTools.GetFormString("newpassword2");
           if (password != password1)
           {
               AddErrLine("两次密码不一致");
           }
           else if (password.Length < 6)
           {
               AddErrLine("密码长度不能小于6位");
           }
           else
           {
               _User.Password = Utils.MD5(password, 16);

               succ = UserBLL.Instance.UpdateUserPassword(_User);
               if (succ)
               {
                   AddMsgLine("密码设置成功");
                   AddMsgLine("页面将自动跳回登录页面");
                   this.SetUrl("/User/Login.aspx");
                   this.SetMetaRefresh();
               }
           }




       }

       private void HandleCommon()
       {
           int id = RequestTools.GetQueryInt("id", -1);
           if (id <= 0)
           {
               AddErrLine("无效的页面");
           }
           else
           {

               _User = UserBLL.Instance.GetUserInfo(id);
               this.AddLocation("重置密码", "/User/SetPassword");
           }
       }
    }
}
